import axios from "axios";
// import { showDialog, showNotify } from 'vant';
import router from '@/router/index.js'
import setting from "@/utils/setting.js";
import {ElNotification} from "element-plus";

const request = axios.create({
    baseURL: 'https://tiktek.top',
    timeout: 10000
})

request.interceptors.request.use(config => {
    const token = sessionStorage.getItem('token')
    if(token) {
        config.headers['token'] = token
    }
    let lang = localStorage.getItem('lang')
    config.headers['lang'] = lang === 'zh' ? 'zh-cn' : 'en-us'
    return config
}, err => {
    return Promise.reject(err)
})

request.interceptors.response.use(res => {
    // 未设置状态码则默认成功状态
    const code = res.data.code;
    switch(code) {
        case 1:
            return res.data
        case 401:
            // showDialog({
            //     title: '提示',
            //     message: `${res.data.msg}`,
            // }).then(() => {
            //     sessionStorage.removeItem('token')
            //     // router.replace('/login')
            //     routerPush('/login')
            // });
            ElNotification({
                title: 'warning',
                message: res.data.msg,
                type: 'warning',
            })
            router.push('/login')
            return
        case 0:
            // showNotify({ type: 'warning', message: `${res.data.msg}` });
            ElNotification({
                title: 'error',
                message: res.data.msg,
                type: 'error',
            })
            return Promise.reject(res.data)
        default:
            // showNotify({ type: 'danger', message: `${res.data.msg}` });
            return Promise.reject(res.data)
    }
}, err => {
    return Promise.reject(err)
})

export default request